<?php
namespace app\service;
// +----------------------------------------------------------------------
// | bbcThink
// +----------------------------------------------------------------------
// | Copyright (c) 2025 http://bbcweb.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: bbcweb <22745529@qq.com>
// +----------------------------------------------------------------------
use think\facade\Db;

//产品
class ProductService
{
    public static function info($param)
    {
        $data=cache('goods|info_'.$param['id']);
        if(empty($data))
        {
            $field=empty($param['field'])?'id,name,price,unit,group_price,contents,vip_price,stock,yongjin,sell_count,service_time,images':$param['field'];
            $data=Db::name('product')->where([['id','=',$param['id']]])->field($field)->find();
            cache('goods|info_'.$param['id'],$data);
        }
        return $data;
    }
    //获取会员价
    public static function getvipprice($param)
    {
        $vip_price=0;
        $product=Db::name('product')->where(['id'=>intval($param['product_id'])])->find();
        if($product)
        {
            $price=$product['vip_price']>0?$product['vip_price']:$product['price'];
            $vip_price+=$price*$param['num'];
        }
        return $vip_price;
    }
    public static function datalist($param = null)
    {
        $where = array();
        if (!empty($param['where'])) {
            $where = $param['where'];
        }
        $order = 'id desc';
        if (!empty($param['order'])) {
            $order = $param['order'];
        }
        $field = '*';
        if (!empty($param['field'])) {
            $field = $param['field'];
        }
        $datalist = Db::name('product')->where($where)->field($field)->order($order)->paginate(['list_rows' => 10, 'query' => $param['query']]);
        $data_list = array();
        foreach ($datalist as $k => $v) {
            $data_list[] = self::handleData($v);
        }
        return ['datalist' => $data_list, 'total' => $datalist->total(), 'pager' => $datalist->render()];
    }
    public static function all($param = null)
    {
        $where = array();
        if (!empty($param['where'])) {
            $where = $param['where'];
        }
        $order = 'id desc';
        if (!empty($param['order'])) {
            $order = $param['order'];
        }
        $field = '*';
        if (!empty($param['field'])) {
            $field = $param['field'];
        }
        $datalist = Db::name('product')->where($where)->field($field)->order($order)->select()->toarray();
        $data_list = array();
        foreach ($datalist as $k => $v) {
            $data_list[] = self::handleData($v);
        }
        return $data_list;
    }
    public static function somelist($param = null)
    {
        $where = array();
        if (!empty($param['where'])) {
            $where = $param['where'];
        }
        $order = 'id desc';
        if (!empty($param['order'])) {
            $order = $param['order'];
        }
        $field = '*';
        if (!empty($param['field'])) {
            $field = $param['field'];
        }
        if(isset($param['top']))
        {
            $datalist = Db::name('product')->where($where)->field($field)->order($order)->limit($param['top'])->select()->toarray();
        }
        else
        {
            $datalist = Db::name('product')->where($where)->field($field)->order($order)->select()->toarray();
        }
        
        $data_list = array();
        foreach ($datalist as $k => $v) {
            $data_list[] = self::handleData($v);
        }
        return $data_list;
    }
    public static function handleData($v)
    {
        if(isset($v['add_time']))
        {
            $v['add_time'] = formatdate($v['add_time']);
        }
        if(isset($v['update_time']))
        {
            $v['update_time'] = formatdate($v['update_time']);
        }
        if(isset($v['images']))
        {
            $v['images_v'] = $v['images'];
            $v['images'] = handleImg($v['images']);
            $v['image'] = empty($v['images'])?'':$v['images'][0];
        }
        if(isset($v['spec']))
        {
            $v['spec'] = empty($v['spec'])?'':$v['spec'];
        }
        if(isset($v['place']))
        {
            $v['place'] = empty($v['place'])?'':$v['place'];
        }
        if(isset($v['category_id']))
        {
            $v['unit'] = empty($v['unit'])?'':$v['unit'];
        }
        if(isset($v['category_id']))
        {
            $v['category'] = Db::name('ProductCategory')->where('id', $v['category_id'])->find();
        }
        if(!empty($v['shop_id']))
        {
            $v['shop'] = array();
            if (!empty($v['shop_id'])) {
                $v['shop'] = Db::name('danwei')->where('id', $v['shop_id'])->value('name', '');
            }
        }
        if(isset($v['price']))
        {
            $v['price']=floatval($v['price']);
        }
        return $v;
    }
}
